package com.xuetao.spider.task;

import com.xuetao.spider.domain.PostInfo;
import com.xuetao.spider.util.ApiConnector;
import com.xuetao.spider.util.SleepUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author xuetao wang
 * @Date 2021/3/31
 */
public class PostTask extends BaseTask {



    public static List<Element> postDocList = new ArrayList();
    public static List<PostInfo> postInfoList = new ArrayList();

    static {
        listPosts();
    }

    @Override
    public void doTask() {
        getPosts();
        getPostInfo();
    }

    public static List<Element> listPosts() {
        String html = ApiConnector.get("https://www.xiaohongshu.com/user/profile/58c4fcde6a6a691cfb4087bb");
        Document document = Jsoup.parse(html);
        document.select("div.note-column")
                .forEach(el -> {
                    el.select("div.note")
                            .forEach(note -> postDocList.add(note));
                });

        return postDocList;
    }

    public List<PostInfo> getPosts() {

        postDocList.parallelStream().forEach(post -> {
            PostInfo postInfo = new PostInfo();

            String title = post.select("div.note-info")
                    .select("a.info").first().text();

            String author = post.select("div.note-append")
                    .select("span.name").first().text();

            String likes = post.select("div.note-append")
                    .select("span.likes").first().text();

            String url = post.select("div.note-info")
                    .select("a").first().attr("href");

            postInfo.setTitle(title);
            postInfo.setAuthor(author);
            postInfo.setLikeNumber(likes);
            postInfo.setPostUrl(url);

            postInfoList.add(postInfo);

        });

        return postInfoList;
    }

    public void getPostInfo() {
        postInfoList.parallelStream().forEach(post -> {
            String html = ApiConnector.get(host + post.getPostUrl());
            Document document = Jsoup.parse(html);

            String postContent = document.select("div.content").text();
            post.setContent(postContent);

            logger.info("post info -> {}", post);

            //Random sleep
            SleepUtils.sleep();
        });
    }
}
